<?php
/**
 * Created by PhpStorm.
 * User: idcu
 * Date: 2016/3/2
 * Time: 22:30
 */
namespace Assistant\Controller;
use Admin\Controller\AdminController;

class FamilyController extends AdminController
{
    protected $searchRule = array(
//        'no' => array('exp'=>'eq','regex'=>'/^\d+$/'),
//        'address' => array('exp'=>'like','regex'=>'/.+/')
        'no' => array('/^\d+$/','like','%','%'),
        'address' => array('/.+/','like','%','%'),
    );

    public function add(){
        $req = $this->getReq();
        $param = $this->getParam();
        if($param){
            $room = M('Room')->find($param['room_id']);
            $param['no'] = $param['no'] ? $param['no'] : sprintf('%02d',($room['children_count']+1));
            $modelName = $this->modelName;
            $field = array();
            $relation = array();
            $create = $field ? D($modelName)->field($field)->create($param,1) : D($modelName)->create($param,1);
            if($create){
                $data = D($modelName)->relation($relation)->add();
                if($data){
                    $pk = D($modelName)->getPk();
                    if($req['customer_id']){
                        $data['family_member'] = D('FamilyMember')->data(array('name'=>$req['customer_name'],'prefix'=>'户主','customer_id'=>$req['customer_id'],'family_id'=>$data))->add();
                    }
                    $res['stat'] = 1;
                    if($pk){
                        $res['pk'] = $data;
                    }
                    $res['data'] = $data;
                    $res['msg'] = '新增数据1条。';
                }else{
                    $res['stat'] = 0;
                    $res['msg'] = '新增数据失败！';
                }
            }else{
                if($req['customer_id'] && $req['room_id']){
                    $family = D('Family')->where(array('room_id'=>$req['room_id']))->find();
                    $customer = D('Customer')->find($req['customer_id']);
                    $family_member['customer_id'] = $req['customer_id'];
                    $family_member['family_id'] = $family['id'];
                    $family_member['name'] = $customer['name'];
                    $family_member['customer_id'] = $req['customer_id'];
                    $family_member['prefix'] = '成员';
                    $family_member['create_time'] = time();
                    $family_member['modify_time'] = time();
                    $family_member['is_checked'] = 0;
                    if(D('FamilyMember')->data($family_member)->add()){
                        $res['stat'] = 1;
                        $res['msg'] = '新增客户到家庭成功！';
                    }else{
                        $res['stat'] = 0;
                        $res['msg'] = D('FamilyMember')->getError();
                    }
                }else{
                    $res['stat'] = 0;
                    $res['msg'] = D($modelName)->getError();
                }
            }
        }else{
            $res['stat'] = 0;
            $res['msg'] = '请提交参数！';
        }
        $this->ajaxReturn($res);
    }

    public function update(){
        $req = $this->getReq();
        $param = $this->getParam();
        if($param){
//            $room = M('Room')->find($param['room_id']);
//            $param['no'] = $param['no'] ? $param['no'] : sprintf('%02d',($room['children_count']+1));
            $modelName = $this->modelName;
            $pk = D($modelName)->getPk();
            $field = array();
            $relation = array();
//            $where = array(); //自定义
            $create = $field ? D($modelName)->field($field)->create($param,2) : D($modelName)->create($param,2);

            if($create){
                if($param[$pk]){
                    $data = D($modelName)->relation($relation)->save();
                }else{
//                    $data = D($modelName)->where($where)->relation($relation)->save();
                }
                if($data){
                    $res['stat'] = 1;
                    $res['data'] = $data;
                    $res['msg'] = '更新数据成功。';
                }else{
                    $res['stat'] = 0;
                    $res['msg'] = '更新数据失败！';
                }
            }else{
                $res['stat'] = 0;
                $res['msg'] = D($modelName)->getError();
            }
        }else{
            $res['stat'] = 0;
            $res['msg'] = '请提交参数！';
        }
        $this->ajaxReturn($res);
    }

    public function search(){
        $param = $this->getParam();
        $uid = session(C('USER_AUTH_KEY'));
        $zoneId = getModelId('Family', $uid);
        $param['id'] = array('in', $zoneId);
        $modelName = $this->modelName;
        $scope = array(
            'order' => array('status'=>'desc','no')
        );
        $relation = array();
        $search = $this->getSearch();
        $page = $this->getPage();
        $data = D($modelName)->where($param)->where($search)->scope($scope)->relation($relation)->page($page[0],$page[1])->select();
        if($data){
            $data = timeToStr('Y-m-d',$data);
            $res['stat'] = 1;
            $res['data'] = $data;
            $res['count'] = $data = D($modelName)->where($param)->where($search)->scope($scope)->count();
            $res['msg'] = '查找到1条数据。';
        }elseif($data == null){
            $res['stat'] = 1;
            $res['data'] = array();
            $res['count'] = 0;
            $res['msg'] = '查找到0条数据。';
        }else{
            $res['stat'] = 0;
            $res['data'] = $data;
            $res['msg'] = '查找失败！';
        }
        $res['recordsTotal'] = $res['count'];
        $res['recordsFiltered'] = $res['count'];
        $this->ajaxReturn($res);
    }

    public function delete(){
        $param = $this->getParam();
        if($param){
            $modelName = $this->modelName;
            $pk = D($modelName)->getPk();
            $relation = array('family_member');
            if($param[$pk]){
                $data = D($modelName)->relation($relation)->delete($param);
            }else{
                $data = D($modelName)->where($param)->relation($relation)->delete();
            }
            if($data){
                $res['stat'] = 1;
                $res['data'] = $data;
                $res['msg'] = '删除数据'.$data.'条。';
            }else{
                $res['stat'] = 0;
                $res['msg'] = '删除数据失败！';
            }
        }else{
            $res['stat'] = 0;
            $res['msg'] = '请提交参数！';
        }
        $this->ajaxReturn($res);
    }

    public function status(){
        $req = $this->getReq();
        $id = strToWhere($req['id']);
        $status = $req['status'];
        if($id && isset($status) && $status!=''){
            $modelName = $this->modelName;
            $dsupStat = D($modelName)->getDsupStat('Room',$id);
            if($dsupStat == -1 || $dsupStat == 0){
                $res['stat'] = 0;
                $res['msg'] = '该元素的父元素状态异常，禁止操作状态！';
            }else{
                $hasDsub = D($modelName)->hasDsub('FamilyMember',array('family_id'=>$id));
                if($hasDsub && $status==-1){
                    $res['stat'] = 0;
                    $res['msg'] = '该元素下有子元素存在，禁止删除！';
                }else{
                    $data = D($modelName)->where(array('id'=>$id))->save(array('status'=>$status));
                    if($data){
                        if($status == 0){
                            D('FamilyMember')->where(array('family_id'=>$id))->save(array('status'=>$status));
                        }
                        $res['stat'] = 1;
                        $res['data'] = $data;
                        $res['msg'] = '更新数据成功。';
                    }else{
                        $res['stat'] = 0;
                        $res['msg'] = '更新数据失败！';
                    }
                }
            }
        }else{
            $res['stat'] = 0;
            $res['msg'] = '请提交参数！';
        }
        $this->ajaxReturn($res);
    }
}